package main.二叉树;

import common.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tmh
 * @date 2024/7/22 14:50
 * @description
 */
public class T114二叉树展开为链表 {
    List<Integer> result = new ArrayList<>();

    /**
     * 先来进行一个先序遍历，然后再来构造一个链表
     * @param root
     */
    public void flatten(TreeNode root) {
        dfs(root);
        TreeNode p = root;
        for (int i = 1; i < result.size(); i++) {
            p.right = new TreeNode(result.get(i));
            p.left = null;
            p = p.right;
        }
    }

    private void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        result.add(root.val);
        dfs(root.left);
        dfs(root.right);
    }
}
